package com.yjps.system.service;


import com.yjps.common.page.Pager;
import com.yjps.system.dao.UserDao;
import com.yjps.system.dao.UserRoleDao;
import com.yjps.system.model.Role;
import com.yjps.system.model.User;
import com.yjps.system.model.UserRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import sun.security.util.Password;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserService{
	@Resource
	private UserDao userDao;

	@Resource
	private UserRoleDao userRoleDao;

	@Transactional
	public void addUser(User user) {
		System.out.println("user id = .... " + userDao.save(user));
		if(user.getRoles() != null && user.getRoles().size() > 0){
			for(Role role : user.getRoles()){
				UserRole ur = new UserRole();
				ur.setIsValid(1);
				ur.setUserId(user.getId());
				ur.setRoleId(role.getId());
                userRoleDao.save(ur);
			}
		}
	}

	@Transactional
	public void deleteUser(int id) {
       userDao.deleteById(id);
	}

	@Transactional
	public void updateUser(User user) {
		System.out.println("user id " + user.getId() + " user = " + user.getUsername());
        String hql = "update User u set u.username =:username,u.mobile =:mobile,u.email =:email,u.realName =:realName,u.password =:password where u.id =:id";
		Map<String, Object> map = new HashMap<>();
		map.put("username",user.getUsername());
		map.put("password", user.getPassword());
		map.put("mobile",user.getMobile());
		map.put("email",user.getEmail());
		map.put("realName",user.getRealName());
		map.put("id",user.getId());
		userDao.excute(hql, map);

		if(user.getRoles() != null && user.getRoles().size() > 0){
			for(Role role : user.getRoles()){
				UserRole ur = new UserRole();
				ur.setIsValid(1);
				ur.setUserId(user.getId());
				ur.setRoleId(role.getId());
				userRoleDao.save(ur);
			}
		}
	}


	public Pager<User> getUserByPager(Pager<User> pager) throws Exception {
		Map<String, Object> params = new HashMap<>();
		String queryString = "from User t where t.isValid = 1";
		queryString += " order by t.id desc";
		List<User> accounts = userDao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
		pager.setCount(userDao.getTotalCount(queryString, params));
		pager.setList(accounts);
		return pager;
	}
	/**
	 * 1--列出是经纪人的用户 0-所有用户
	 * @param type 类型
	 * @return
	 */
	public List<User> findUserList(String type) {
		String hql = "";
		if(!StringUtils.isEmpty(type) && type.equals("1")) {
			 hql = "from User u where u.isValid = 1 and u.agentsId != 0 ";
		}else{
			hql = "from User u where u.isValid = 1";
		}
		return userDao.find(hql);
	}

	public User findUserByName(String username) {
		String hql = "from User u where u.username = :username";
		Map<String, Object> map = new HashMap<>();
		map.put("username",username);
		return (User)userDao.findUniqueResult(hql, map);
	}

	public User findUser(int id) {
		return  userDao.load(id);
	}

	public User findUserAgentId(int agentId) {
		String hql = "from User u where u.agentsId = :agentsId";
		Map<String, Object> map = new HashMap<>();
		map.put("agentsId",agentId);
		return userDao.findUniqueResult(hql, map);
	}


}
